package com.fccommunity.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptor;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.overlay.WalkRouteOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkPath;
import com.amap.api.services.route.WalkRouteResult;

import java.lang.reflect.Member;
import java.util.List;

/**
 * Created by Administrator on 2015/9/15.
 */
public class MyMapView extends FrameLayout {
    private MapView mapView;
    private AMap aMap;
    RouteSearch routeSearch;
    WalkRouteResult walkRouteResult;
    private Marker loctionMarker;

    public MyMapView(Context context) {
        this(context, null);
    }

    public MyMapView(Context context, AttributeSet attrs) {
        //这里构造方法也很重要，不加这个很多属性不能再XML里面定义
        this(context, attrs, 0);
    }

    public MyMapView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        mapView = new MapView(getContext());
        addView(mapView, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        aMap = mapView.getMap();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        aMap =mapView.getMap();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        aMap=null;
    }

    public void moveToPoint(LatLng latLng) {
        if(aMap!=null){
            aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                    latLng,
                    18));
            if(loctionMarker!=null){
                loctionMarker.remove();
                loctionMarker.destroy();
                Log.d("marker","locationMarker destroy");
            }
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.visible(true);
            markerOptions.draggable(false);
            //markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location_marker));
            loctionMarker= aMap.addMarker(markerOptions);
            Log.d("marker","locationMarker added");
        }

    }

    public void showRoute(LatLng from,final BitmapDescriptor fromDescriptor, LatLng to,final BitmapDescriptor toDescriptor) {
        if(aMap==null){
            return;
        }
        routeSearch = new RouteSearch(getContext());//初始化routeSearch 对象
        routeSearch.setRouteSearchListener(new RouteSearch.OnRouteSearchListener() {
            @Override
            public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

            }

            @Override
            public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {

            }

            @Override
            public void onWalkRouteSearched(WalkRouteResult result, int rCode) {
                try {
                    if(aMap==null){
                        return;
                    }
                    if (rCode == 0) {
                        if (result != null && result.getPaths() != null
                                && result.getPaths().size() > 0) {
                            walkRouteResult = result;
                            WalkPath walkPath = walkRouteResult.getPaths().get(0);
                            aMap.clear();// 清理地图上的所有覆盖物
                            final WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(getContext(),
                                    aMap, walkPath, walkRouteResult.getStartPos(),
                                    walkRouteResult.getTargetPos()) {
                                @Override
                                protected int getWalkColor() {
                                    return Color.parseColor("#99797979");
                                }

                                @Override
                                protected BitmapDescriptor getStartBitmapDescriptor() {
                                    return fromDescriptor;
                                }

                                @Override
                                protected BitmapDescriptor getEndBitmapDescriptor() {
                                    return toDescriptor;
                                }
                            };
                            walkRouteOverlay.setNodeIconVisibility(false);
                            walkRouteOverlay.removeFromMap();
                            if(aMap!=null){
                                post(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            walkRouteOverlay.addToMap();
                                            walkRouteOverlay.zoomToSpan();
                                        }catch (Exception e){
                                            e.printStackTrace();
                                        }

                                    }
                                });
                            }
                        } else {
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        });//设置数据回调监听器
        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(new LatLonPoint(from.latitude, from.longitude), new LatLonPoint(to.latitude, to.longitude));
        RouteSearch.WalkRouteQuery query = new RouteSearch.WalkRouteQuery(fromAndTo, RouteSearch.WalkDefault);//初始化query对象，fromAndTo是包含起终点信息，walkMode是不行路径规划的模式
        routeSearch.calculateWalkRouteAsyn(query);//开始算路
    }
    /**
     *  把附近的活儿宝画到地图里面
     */
    public void drawMarkers(List<Member> memberList,AMap.OnMarkerClickListener listener){
        if(aMap==null){
            return;
        }
        /*IdoAccount mAccount = UserManager.getInstance().getMyAccount();
        aMap.clear();
        for (int i = 0; i < memberList.size(); i++) {
            Member member = memberList.get(i);
            if (mAccount != null && mAccount.id.equals(member.getId() + "")) {
                continue;
            }
            if (member.getLat()!=0) {
                LatLng point = new LatLng(memberList.get(i)
                        .getLat(), memberList.get(i).getLng());
                MarkerOptions options = new MarkerOptions().position(point)
                        .draggable(false);
                // options.title(mlist.get(i).getNikename());

                if (member.getSex().equals("1")) {
                    if (memberList.get(i).getLevel().equals("2")) {
                        options.icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.a1_12));
                    } else {
                        options.icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.a1_08));
                    }
                } else {
                    if (member.getLevel().equals("2")) {
                        options.icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.a1_11));
                    } else {
                        options.icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.a1_07));
                    }
                }
                Marker marker = aMap.addMarker(options);
                marker.setObject(member);
                aMap.setOnMarkerClickListener(listener);
            }
        }*/
    }


    public MapView getMapView() {
        return mapView;
    }

    public BitmapDescriptor getBitmapDescriptor(String sex, String vip) {
        /*if (sex.equals("1")) {
            if (vip.equals("2")) {
                return BitmapDescriptorFactory
                        .fromResource(R.drawable.a1_12);
            } else {
                return BitmapDescriptorFactory
                        .fromResource(R.drawable.a1_08);
            }
        } else {
            if (vip.equals("2")) {
                return BitmapDescriptorFactory
                        .fromResource(R.drawable.a1_11);
            } else {
                return BitmapDescriptorFactory
                        .fromResource(R.drawable.a1_07);
            }
        }*/
        return null;
    }
}
